﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
public partial class Admin_adminClient : System.Web.UI.Page
{
    UserBll userBll = new UserBll();  
    int pagesize = 20;
    public string input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=1' />";
    private static readonly object objSync = new object();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["action"]!=null)
            {
                string action = Request.QueryString["action"].ToString().Trim();
                switch (action)
                {
                    case "del":
                        del();
                        break;
                    case "Freeze":
                        Freeze();
                        break;  
                    case "Question":
                        writeCookie();
                        break;
                    default:
                        break;
                }
            }
        }
        bindUser();
    }
    #region 冻结用户
    public void Freeze() 
    {
        bool state = Convert.ToBoolean(Request.QueryString["state"]);
        int uid = Convert.ToInt32(Request.QueryString["uid"]);
        User user = userBll.GetModel(uid);
        if (user.userID>0)
        {
            if (state == true)
            {
                user.userState = false;
                if (userBll.Update(user))
                {
                    
                }
            }
            else 
            {
                user.userState = true;
                if (userBll.Update(user))
                {
                   
                }
            } 
        }
    }
    #endregion

    #region 删除用户
    public void del() 
    {
        int uid=Convert.ToInt32(Request.QueryString["uid"]);
        QuestionBll questionBll = new QuestionBll();
        FavoriteBll fBll = new FavoriteBll();
        CaseBll caseBll = new CaseBll();
        User user = userBll.GetModel(uid);
        
        lock(objSync)
        {
            questionBll.Delete(user.userName);
            fBll.DeleteByUserID(uid);
            caseBll.Delete(uid);
            userBll.Delete(uid);
        } 
    }
    #endregion

    #region 绑定用户数据--郑同飞
    public void bindUser() 
    {
        if (Request.QueryString["CurrentIndex"]!=null)
        {
            _currentIndex = Convert.ToInt32(Request.QueryString["CurrentIndex"]);
        }
        if (Request.QueryString["userEmail"]!=null)
	    {
    		 _email=Request.QueryString["userEmail"].ToString();
	    }
        if (Request.QueryString["userPhone"]!=null)
	    {
    		 _userphone=Request.QueryString["userPhone"].ToString();
	    }
        if (Request.QueryString["userName"]!=null)
        {
            _username = Request.QueryString["userName"].ToString();
        }
        if (Request.QueryString["userState"]!=null)
        {
            _userState = Request.QueryString["userState"].ToString();
        }
        DataTable dt = userBll.pagingForUser(_currentIndex, pagesize, _username, _email, _userphone,_userState);
        if (dt.Rows.Count==0&&_currentIndex>1)
        {
            --_currentIndex;
            dt = userBll.pagingForUser(_currentIndex, pagesize, _username, _email, _userphone,_userState);
        }
        input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=" + _currentIndex + "' />";
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();
    }
    #endregion

    #region 分页按钮--郑同飞
    public void htmlpaging() 
    {
        string str = null;
        int count = 0;
        count = userBll.getRecordCount(_username, _email, _userphone);
        int pcount = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
        str = "页次：" + _currentIndex + "/" + pcount + "  每页" + pagesize + " 总收录数据" + count + "条";
        str += HtmlControls.pagIng(_currentIndex, pcount, "adminClient.aspx?userName=" + _username + "&userEmail=" + _email + "&userPhone=" + _userphone + "&CurrentIndex", "pageaa", "pagecurrent");
        Response.Write(str);
    }
    #endregion

    #region 状态维护
    private int _currentIndex = 1;
    private string _username = "";
    private string _email = "";
    private string _userphone = "";
    private string _userState = "";
    #endregion

    #region 写入用户的cookie
    public void writeCookie() 
    {
        int uid = Convert.ToInt32(Request.QueryString["uid"]);
        string page = Request.QueryString["page"].ToString();
        User user = userBll.GetModel(uid);
        DotCookie.Add("userName", user.userName);
        DotCookie.Add("userPwd", user.userPwd);
        string url = page;
        Response.Write(url);
        Response.End();
    }
    #endregion    
}
